package io.renren.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.renren.common.exception.RRException;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.app.dao.BsEmployeeDao;
import io.renren.modules.app.dao.BsIntendedCustomersDao;
import io.renren.modules.app.entity.BsEmployeeEntity;
import io.renren.modules.app.entity.BsIntendedCustomersEntity;
import io.renren.modules.app.entity.BsMemberEntity;
import io.renren.modules.app.service.BsIntendedCustomersService;
import io.renren.modules.sys.dao.SysUserDao;
import io.renren.modules.sys.entity.SysUserEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;


@Service("bsIntendedCustomersService")
@Slf4j
public class BsIntendedCustomersServiceImpl extends ServiceImpl<BsIntendedCustomersDao, BsIntendedCustomersEntity> implements BsIntendedCustomersService {

    @Resource
    private BsIntendedCustomersDao intendedCustomersDao ;

    @Resource
    private SysUserDao sysUserDao ;

    @Resource
    private BsEmployeeDao bsEmployeeDao ;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        LambdaQueryWrapper<BsIntendedCustomersEntity> wrapper1 = new LambdaQueryWrapper<>();
        if (params.get("intendedName") != null && !params.get("intendedName").toString().equals("")){
            wrapper1.like(BsIntendedCustomersEntity::getIntendedName,params.get("intendedName"));
        }
        if (params.get("intendedPhone") != null && !params.get("intendedPhone").toString().equals("")){
            wrapper1.like(BsIntendedCustomersEntity::getIntendedPhone,params.get("intendedPhone"));
        }
        if (params.get("intendedFrom") != null && !params.get("intendedFrom").toString().equals("")){
            wrapper1.eq(BsIntendedCustomersEntity::getIntendedFrom,params.get("intendedFrom"));
        }
        if (params.get("intendedBranch") != null && !params.get("intendedBranch").toString().equals("")){
            wrapper1.eq(BsIntendedCustomersEntity::getIntendedBranch,params.get("intendedBranch"));
        }
        if (params.get("intendedOwner") != null && !params.get("intendedOwner").toString().equals("")){
            LambdaQueryWrapper<BsEmployeeEntity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(BsEmployeeEntity::getEmployeeName,params.get("intendedOwner"));
            List<BsEmployeeEntity> list = bsEmployeeDao.selectList(queryWrapper);
            List<String> list1 = new ArrayList<>();
            if (list.size() > 0){
                for (BsEmployeeEntity user : list) {
                    list1.add(user.getEmployeeId().toString());
                }
                wrapper1.in(BsIntendedCustomersEntity::getIntendedOwner,list1);
            }else {
                IPage<BsIntendedCustomersEntity> page = this.page(
                        new Query<BsIntendedCustomersEntity>().getPage(params),
                        new LambdaQueryWrapper<BsIntendedCustomersEntity>().eq(BsIntendedCustomersEntity::getId,-1)
                );
                return new PageUtils(page);
            }

        }
        IPage<BsIntendedCustomersEntity> page = this.page(
                new Query<BsIntendedCustomersEntity>().getPage(params),
                wrapper1
        );
        for (BsIntendedCustomersEntity record : page.getRecords()) {
            LambdaQueryWrapper<BsEmployeeEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BsEmployeeEntity::getEmployeeId,record.getIntendedOwner());
            List<BsEmployeeEntity> sysUserEntities = bsEmployeeDao.selectList(wrapper);
            if (sysUserEntities.size() == 1){
                record.setUserName(sysUserEntities.get(0).getEmployeeName());
            }
        }



        return new PageUtils(page);
    }

    @Override
    public void saveCustomer(BsIntendedCustomersEntity bsIntendedCustomers) {
        try {
            if (StringUtils.isBlank(bsIntendedCustomers.getIntendedOwner())){
                throw new RRException("请输入营销人员编号");
            }
            String intendedOwner = bsIntendedCustomers.getIntendedOwner(); // 营销人员编号
            LambdaQueryWrapper<BsEmployeeEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BsEmployeeEntity::getEmployeeId,intendedOwner);
            List<BsEmployeeEntity> sysUserEntities = bsEmployeeDao.selectList(wrapper);
            if (sysUserEntities.size() < 1){
                throw new RRException("请确认归属营销人员");
            }
            if (!phoneValidate(bsIntendedCustomers.getIntendedPhone())){
                throw new RRException("电话号不合法");
            }
            bsIntendedCustomers.setIntendedOwner(String.valueOf(sysUserEntities.get(0).getEmployeeId()));
            intendedCustomersDao.insert(bsIntendedCustomers);
        }catch (Exception e){
            log.info("报错类及其所在方法：{}","BsIntendedCustomersService --> saveCustomer");
            log.info(e.getMessage());
            throw new RRException(e.getMessage());
        }
    }

    @Override
    public void updateCustomer(BsIntendedCustomersEntity bsIntendedCustomers) {
        try {
            LambdaQueryWrapper<BsEmployeeEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BsEmployeeEntity::getEmployeeId,bsIntendedCustomers.getIntendedOwner());
            List<BsEmployeeEntity> sysUserEntities = bsEmployeeDao.selectList(wrapper);
            if (sysUserEntities.size() < 1){
                throw new RRException("请确认归属营销人员");
            }
            if (!phoneValidate(bsIntendedCustomers.getIntendedPhone())){
                throw new RRException("电话号不合法");
            }
            bsIntendedCustomers.setIntendedOwner(String.valueOf(sysUserEntities.get(0).getEmployeeId()));
            intendedCustomersDao.updateById(bsIntendedCustomers);
        }catch (Exception e){
            log.info("报错类及其所在方法：{}","BsIntendedCustomersService --> saveCustomer");
            log.info(e.getMessage());
            throw new RRException(e.getMessage());
        }
    }

    @Override
    public BsIntendedCustomersEntity getIntendById(Long id) {

        BsIntendedCustomersEntity intendedCustomers = intendedCustomersDao.selectById(id);

        LambdaQueryWrapper<BsEmployeeEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BsEmployeeEntity::getEmployeeId,intendedCustomers.getIntendedOwner());
        List<BsEmployeeEntity> sysUserEntities = bsEmployeeDao.selectList(wrapper);
        if (sysUserEntities.size() == 1){
            intendedCustomers.setIntendedOwner(sysUserEntities.get(0).getEmployeeId());
        }
        return intendedCustomers;
    }

    //校验电话号公共方法
    private boolean phoneValidate(String phoneNumber){
        if (phoneNumber != null && !phoneNumber.isEmpty()){
            boolean matches = Pattern.matches(
                    "^1[3-9]\\d{9}$|" +
                            "^1[3-9]\\d{1}[-\\s]\\d{4}[-\\s]\\d{4}$|" +
                            "^\\(1[3-9]\\d{1}\\)\\d{4}-\\d{4}$|" +
                            "^(?:\\(\\+\\d{2}\\)|\\+\\d{2})(\\d{11})$|" +
                            "^0\\d{3}-\\d{7}$|" +
                            "^0\\d{2}-\\d{8}$", phoneNumber);

            return matches;
        }
        return false;
    }

}
